package com.woniuxy.house.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.broker.Broker;
import com.woniuxy.entity.house.Secondhouse;
import com.woniuxy.entity.house.dto.SecondHouseTypeDto;
import com.woniuxy.entity.house.dto.SecondhouseCommentDto;
import com.woniuxy.entity.house.dto.SecondhouseDto;
import com.woniuxy.entity.house.dto.SecondhouseQO;
import com.woniuxy.house.feign.BrokerService;
import com.woniuxy.house.mapper.SecondhouseMapper;
import com.woniuxy.house.service.SecondhouseService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangyu
 * @since 2021-03-19
 */
@Service
public class SecondhouseServiceImpl extends ServiceImpl<SecondhouseMapper, Secondhouse> implements SecondhouseService {

    @Resource
    private SecondhouseMapper secondhouseMapper;

    @Resource
    private BrokerService brokerService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    // 分页查询所有二手房信息
    @Override
    public Page<Secondhouse> findPage(int current, int size) {
        return secondhouseMapper.selectPage(new Page<>(current,size),null);
    }

    //根据二手房id查询单个二手房及其户型，楼盘，评论信息
    @Override
    public SecondhouseDto findOneById(int id) {
        SecondhouseDto secondhouseDto = secondhouseMapper.findOneById(id);
        if(secondhouseDto != null){
            List<SecondhouseCommentDto> secondhouseCommentDtos = secondhouseDto.getSecondhouseCommentDtos();
            for (SecondhouseCommentDto secondhouseCommentDto : secondhouseCommentDtos) {
                Integer bid = secondhouseCommentDto.getBid();
                Broker broker = brokerService.getSingleBroker(bid).getData();
                secondhouseCommentDto.setBorderName(broker.getName());
                secondhouseCommentDto.setBorderImage(broker.getAvatar());
            }
            // 根据id查询获取认证经纪人
            Broker cbBroker = brokerService.getSingleBroker(secondhouseDto.getCbid()).getData();
            // 根据id查询获取维护经纪人
            Broker mbBroker = brokerService.getSingleBroker(secondhouseDto.getMbid()).getData();
            // 根据id查询获取实勘经纪人
            Broker ibBroker = brokerService.getSingleBroker(secondhouseDto.getIbid()).getData();

            secondhouseDto.setCbName(cbBroker.getName()).setCbImage(cbBroker.getAvatar());
            secondhouseDto.setMbName(mbBroker.getName()).setMbImage(mbBroker.getAvatar());
            secondhouseDto.setIbName(ibBroker.getName()).setIbImage(ibBroker.getAvatar());
        }
        return secondhouseDto;
    }

    // 根据二手房id软删除租房信息，将status修改为1
    @Override
    public boolean deleteOneById(int id) {
        return secondhouseMapper.deleteOneById(id) > 0;
    }

    // 根据二手房id修改单个二手房信息
    @Override
    public boolean updateBySid(Secondhouse secondhouse) {
        UpdateWrapper<Secondhouse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",secondhouse.getId());
        return secondhouseMapper.update(secondhouse,updateWrapper) > 0;
    }

    // 根据条件分页查询（包括排序）
    @Override
    public Page<SecondHouseTypeDto> findLike(int current, int size, SecondhouseQO secondhouseQO) {
        return secondhouseMapper.findLike(new Page<>(current,size),secondhouseQO);
    }

    // 预加载 将热点数据提前放入redis，例如 分页查询二手房第一页数据(十个)
//    @Override
//    public void afterPropertiesSet() throws Exception {
//        Page<SecondHouseTypeDto> page = secondhouseMapper.findLike(new Page<>(1, 10), new SecondhouseQO());
//        redisTemplate.opsForValue().set("find:com.woniuxy.house.service.impl.SecondhouseServiceImplfindLike:1:10:" +
//                "SecondhouseQO(description=null, minFloor=null, maxFloor=null, minPrice=null, maxPrice=null, minArea=null, " +
//                "maxArea=null, houseHead=null, decorate=null, elevator=null, sortPrice=null, sortArea=null)",page);
//    }

}
